package com.fili.core.keygen;

import com.fili.gui.components.Bevel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 * User: fili
 * Date: Oct 2, 2006
 * Time: 10:13:37 PM
 */
public class KeyGenMain extends JFrame implements ActionListener
{
    private JTextField edName;
    private JComboBox cbNrUsc;
    private JButton btnGenerate;
    private JTextField edKey;
    private JButton btnPrint;
    private JCheckBox cbISPM;
    private JCheckBox cbNormal;

    public KeyGenMain()
    {
        setSize(287, 195);
        setLayout(null);
        setTitle("Keygen uscator");

        JLabel lbl = new JLabel("Nume: ");
        lbl.setBounds(8, 11, 49, 13);
        add(lbl);

        lbl = new JLabel("Nr Uscatoare");
        lbl.setBounds(8, 44, 81, 13);
        add(lbl);

        Bevel bvl = new Bevel();
        bvl.setBounds(8, 120, 258, 2);
        add(bvl);

        edName = new JTextField("");
        edName.setBounds(48, 8, 217, 21);
        add(edName);

        cbNrUsc = new JComboBox(new String[]{"1","2","3","4","5","6","7","8"});
        cbNrUsc.setSelectedIndex(7);
        cbNrUsc.setBounds(90, 40, 49, 21);
        add(cbNrUsc);

        cbISPM = new JCheckBox("ISPM");
        cbISPM.setBounds(8, 72, 97, 17);
        add(cbISPM);

        cbNormal = new JCheckBox("Normal");
        cbNormal.setBounds(8, 96, 97, 17);
        add(cbNormal);

        btnGenerate = new JButton("Genereaza");
        btnGenerate.addActionListener(this);
        btnGenerate.setMargin(new Insets(0, 0, 0, 0));
        btnGenerate.setBounds(192, 88, 75, 25);
        add(btnGenerate);

        edKey = new JTextField("edKey");
        edKey.setBounds(8, 128, 201, 21);
        edKey.setEditable(false);
        edKey.setFont(new Font("Courier",Font.PLAIN, 14));
        add(edKey);

        btnPrint = new JButton("Print");
        btnPrint.addActionListener(this);
        btnPrint.setMargin(new Insets(0, 0, 0, 0));
        btnPrint.setBounds(216, 128, 51, 21);
        btnPrint.setEnabled(false);
        add(btnPrint);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (btnGenerate.equals(e.getSource()))
        {
            edKey.setText(KeyEncoder.encode(edName.getText().trim(), cbNrUsc.getSelectedIndex()+1, cbISPM.isSelected(), cbNormal.isSelected(), KeyEncoder.getSeed()));
        }
        if (btnPrint.equals(e.getSource()))
        {
            System.out.println(KeyEncoder.match(edName.getText().trim(), edKey.getText()));
        }
    }

    public static void main(String[] args)
    {
        new KeyGenMain();
    }
}
